package sng.utils
{
	import de.polygonal.ds.Array2;
	
	import flash.geom.Point;
	
	import sng.modules.view.components.city.logic.City;
	import sng.modules.view.components.city.logic.unit.BaseBuilding;

	public class IsoUtil
	{
		/**
		 * 0为空置状态，1为一般建筑，2为道路， 9为未开垦状态
		 * 
		 */		
		
		/**
		 * 
		 * @param map	地图的可建数组
		 * @param x		建筑的定位x
		 * @param y		建筑的定位y
		 * @param rows	建筑占地的列数
		 * @param cols	建筑占地的行数
		 * @return 
		 * 
		 */		
		public static function getBuildAble(map:Array2, x:int, y:int, rows:int, cols:int):Boolean
		{
			var x_left:int 	= x - Math.floor(rows/2);
			var y_top:int 	= y - Math.floor(cols/2);
//			trace(x, y, x_left, y_top, rows, cols);
			for(var i:int=0;i<rows;i++)
			{
				for(var j:int=0;j<cols;j++)
				{
					if(map.get(x_left+i, y_top+j)!=0)
						return false;
					if(x_left+i<0 || y_top+j<0 || x_left+i>map.width-1 || y_top+j>map.height-1)
						return false;
				}
			}
			return true;
		}
		
		/**
		 * 扩展地块(expand)是否能建逻辑判断
		 */		
		public static function getBuildAbleExpand(map:Array2, x:int, y:int, rows:int, cols:int):Boolean
		{
			var x_left:int 	= x - Math.floor(rows/2);
			var y_top:int 	= y - Math.floor(cols/2);
			//			trace(x, y, x_left, y_top, rows, cols);
			for(var i:int=0;i<rows;i++)
			{
				for(var j:int=0;j<cols;j++)
				{
					if(map.get(x_left+i, y_top+j)!=null)
						return false;
				}
			}
			return true;
		}
		
		/**
		 * 获取建筑本身所占据的位置
		 * @param x
		 * @param y
		 * @param rows
		 * @param cols
		 * @return 
		 */		
		public static function getBuildHoldPositions(x:int, y:int, rows:int, cols:int):Array
		{
			var posArray:Array = new Array();
			var x_left:int 	= x - rows;
			var y_top:int 	= y - cols;
			//			trace(x, y, x_left, y_top, rows, cols);
			for(var i:int=0;i<rows;i++)
			{
				for(var j:int=0;j<cols;j++)
				{
					posArray.push(new Point(x_left+i, y_top+j));
				}
			}
			return posArray;
		}
		
		/**
		 * 获取建筑周围的连接位置
		 * @param x
		 * @param y
		 * @param rows
		 * @param cols
		 * @return 
		 * 如3*3格子 o是建筑物占的格子 #是建筑物相连的格子
		 * 			 ###
		 * 			#ooo#
		 * 			#ooo#
		 * 			#ooo#
		 * 			 ###
		 * 
		 */		
		private static function getBuildSidePositions(x:int, y:int, rows:int, cols:int):Array
		{
			var posArray:Array = new Array();
			var i:int;
			for(i=1;i<rows+1;i++)
			{
				posArray.push(new Point(x-i, y));
				posArray.push(new Point(x-i, y-cols-1));
			}
			for(i=1;i<cols+1;i++)
			{
				posArray.push(new Point(x, y-i));
				posArray.push(new Point(x-rows-1, y-i));
			}
			return posArray;
		}
		
		/**
		 * 返回建筑id数组，详细看 getBuildSidePositions函数
		 */		
		public static function getBuildSideIdArray(x:int, y:int, rows:int, cols:int):Array
		{
			var idArray:Array = new Array();
			var posArray:Array = getBuildSidePositions(x, y, rows, cols);
			var leng:uint = posArray.length;
			var point:Point;
			var buildingId:uint;
			var i:uint;
			for(i=0;i<leng;i++)
			{
				point = posArray[i];
				buildingId = City.buildingIdArray.get(point.x, point.y);
				if(buildingId==0) continue;
				if(idArray.indexOf(buildingId)!=-1) continue;
				idArray.push(buildingId);
			}
			
			return idArray;
		}
		
		/**
		 * 获取装饰性建筑周围的加成影响位置
		 * @param x
		 * @param y
		 * @param rows
		 * @param cols
		 * @return 
		 * 策划设定加成影响都是3格
		 * 如3*3格子 o是建筑物占的格子 #是影响范围的格子
		 * 			#########
		 * 			#########
		 * 			#########
		 * 			###ooo###
		 * 			###ooo###
		 * 			###ooo###
		 * 			#########
		 * 			#########
		 * 			#########
		 */		
		private static function getBuildBonusPositions(x:int, y:int, rows:int, cols:int):Array
		{
			var posArray:Array = new Array();
			var x_left:int 	= x - rows - 3;
			var y_top:int 	= y - cols - 3;
			for(var i:int=0;i<rows+6;i++)
			{
				for(var j:int=0;j<cols+6;j++)
				{
					if(i>=3 && j>=3 && i<3+rows && j<3+cols)
						continue;
					posArray.push(new Point(x_left+i, y_top+j));
				}
			}
			return posArray;
		}
		
		/**
		 * 返回建筑id数组，详细看 getBuildBonusPositions函数
		 */		
		public static function getBuildBonusIdArray(x:int, y:int, rows:int, cols:int):Array
		{
			var idArray:Array = new Array();
			var posArray:Array = getBuildBonusPositions(x, y, rows, cols);
			var leng:uint = posArray.length;
			var point:Point;
			var buildingId:uint;
			var i:uint;
			for(i=0;i<leng;i++)
			{
				point = posArray[i];
				buildingId = City.buildingIdArray.get(point.x, point.y);
				if(buildingId==0) continue;
				if(idArray.indexOf(buildingId)!=-1) continue;
				idArray.push(buildingId);
			}
			
			return idArray;
		}
		
		/**
		 * 
		 * 
		 */
		public static function updateAllMapDataInfo(building:BaseBuilding, isAdd:Boolean):void
		{
			if(building==null)
				return;
			IsoUtil.changeMapDataByBuilding(building,isAdd);
			building.cityView.groundScene.updateBuildingInfoGridArr(building,isAdd);
		}
		
		/**
		 * 增删建筑对buildingTypeArray的修改
		 * @param building
		 * 
		 */		
		private static function changeMapDataByBuilding(building:BaseBuilding, isAdd:Boolean):void
		{
			var x:int = building.uVO.x;
			var y:int = building.uVO.y;
			var rows:int = building.vo.rows;
			var cols:int = building.vo.cols;
			var value:int = building.vo.buildingTypeId==BuildingUtil.ROAD_TYPE?2:1;
			if(!isAdd) value = 0;
			var id:int = Number(building.id);
			if(!isAdd) id = 0;
			
			var x_left:int 	= x - rows;
			var y_top:int 	= y - cols;
			//			trace(x, y, x_left, y_top, rows, cols);
			for(var i:int=0;i<rows;i++)
			{
				for(var j:int=0;j<cols;j++)
				{
					City.buildingTypeArray.set(x_left+i, y_top+j, value);
					City.buildingIdArray.set(x_left+i, y_top+j, id);
//					trace(x_left+i,y_top+j,value);
					
					//如果是道路 royjin 20100715
					if(building.vo.buildingTypeId==BuildingUtil.ROAD_TYPE)
					{
						building.cityView.astarGrid.setWalkable(x_left+i, y_top+j, isAdd);
						if(isAdd)
							building.city.roadManager.AddRoad(building);
						else
							building.city.roadManager.DelRoad(building);
					}

				}
			}
		}
	}
}